#ifndef CMD_CENTRE_H
#define CMD_CENTRE_H

#include "cmd_view.h"
#include "cmd_console_reader.h"

#include <string>
#include <map>
#include <boost/bind.hpp>

namespace vpm
{
	namespace cmo 
	{
		static const std::string cmd_help = "help";
		static const std::string cmd_ls   = "ls";
		static const std::string cmd_cd_view = "cd (view)";
		static const std::string cmd_about   = "about";
		static const std::string cmd_exit   = "exit";

		class CmdCentre 
		{	
			typedef std::map<std::string , CmdViewPtr> CmdViewMap;
			typedef std::set <CmdUserSessionPtr> CmdUserSessions; 

			CmdCentre();

		public:					

			void start()
			{
				m_cmdReader.reset(new  CmdConsoleReader());
				m_viewMap.insert(std::make_pair(m_defaultView->view_name(),m_defaultView));
				m_cmdReader->read();
			}

			void add_view(const std::string & view, const std::string & prompt= ">")
			{
				CmdViewPtr pCmdView(new CmdView()); 
				pCmdView->view_prompt(prompt);
				m_viewMap.insert(std::make_pair(view,pCmdView));
			}

// 			void add_command(const std::string & view, CmdExecutorPtr pExecutor)
// 			{
// 				CmdViewPtr pView = this->get_view(view); 
// 				if (pView)
// 				{		
// 					pView->add_command(pExecutor->command(),pExecutor);
// 				}
// 			}
// 
// 			void add_command(const std::string & view, const std::string & cmd, CmdExecutorPtr pExecutor)
// 			{
// 				CmdViewPtr pCmdView = this->get_view(view); 
// 				if (pCmdView)
// 				{		
// 					pCmdView->add_command(cmd,pExecutor);
// 				}
// 			}

			CmdViewPtr get_view(const std::string & view = "default")
			{
				CmdViewMap::iterator itr = m_viewMap.find(view);
				if (itr != m_viewMap.end())
				{
					return itr->second ;
				}
				return m_defaultView; 
			}

			void change_view(const std::string& view)
			{
				CmdViewPtr pView = this->get_view(view);
				//m_cmdReader->change_view(pView);
			}

			CmdUserSessionPtr acquire_user_session(CmdViewPtr pView)
			{				
				CmdUserSessionPtr session(new CmdUserSession(pView));
				m_userSessions.insert(session); 
				return session; 
			}

			void release_user_session(CmdUserSessionPtr session)
			{
				m_userSessions.erase(session);
			}

			static CmdCentre & instance();

		private:
			CmdUserSessions  m_userSessions; 
			CmdViewMap m_viewMap; 
			CmdViewPtr    m_defaultView; 

			boost::shared_ptr<CmdReader > m_cmdReader; 
			static CmdCentre * m_instance; 
		};
	}
}

  

#endif // 
